#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

const size_t MAX_HEAP = 1024;

static char* mem_heap;
static char* mem_brk;
static char* mem_max_addr;

void mem_init() {
    mem_heap = (char*)malloc(MAX_HEAP);
    mem_brk = mem_heap;
    mem_max_addr = (char*)mem_heap+MAX_HEAP;
}

void* mem_sbrk(size_t incr) {
    void* old_brk = (void*)mem_brk;
    if(incr < 0 || mem_brk+incr > mem_max_addr) {
        errno = ENOMEM;
        fprintf(stderr, "mem_sbrk() faild!\n");
        return (void*)-1;
    }
    mem_brk += incr;
    return old_brk;
} 